<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>python收发邮件 - Lglspace‘s Blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="lglspace" /><meta name="description" content="发送邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import smtplib from email.mime.text import MIMEText from_addr = &amp;#39;&amp;#39; password = &amp;#39;&amp;#39; to_addr = smtp_server = &amp;#39;smtp.163.com&amp;#39; msg = MIMEText(&amp;#39;python send email test...&amp;#39;,&amp;#39;plain&amp;#39;,&amp;#39;utf-8&amp;#39;) msg[&amp;#39;From&amp;#39;] = &amp;#39;abc&amp;lt;%s&amp;gt;&amp;#39;% from_addr msg[&amp;#39;To&amp;#39;] = &amp;#39;test@test.com&amp;#39; msg[&amp;#39;Subject&amp;#39;] = &amp;#39;Python邮件测试&amp;#" /><meta name="keywords" content="lglspace, blog, lgl" />






<meta name="generator" content="Hugo 0.79.0 with theme even" />


<link rel="canonical" href="https://lglspace.cn/post/python/python-email/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/even-custom.css">


<meta property="og:title" content="python收发邮件" />
<meta property="og:description" content="发送邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import smtplib from email.mime.text import MIMEText from_addr = &#39;&#39; password = &#39;&#39; to_addr = smtp_server = &#39;smtp.163.com&#39; msg = MIMEText(&#39;python send email test...&#39;,&#39;plain&#39;,&#39;utf-8&#39;) msg[&#39;From&#39;] = &#39;abc&lt;%s&gt;&#39;% from_addr msg[&#39;To&#39;] = &#39;test@test.com&#39; msg[&#39;Subject&#39;] = &#39;Python邮件测试&#" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://lglspace.cn/post/python/python-email/" />
<meta property="article:published_time" content="2019-06-06T10:10:10+08:00" />
<meta property="article:modified_time" content="2019-06-06T10:10:10+08:00" />
<meta itemprop="name" content="python收发邮件">
<meta itemprop="description" content="发送邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import smtplib from email.mime.text import MIMEText from_addr = &#39;&#39; password = &#39;&#39; to_addr = smtp_server = &#39;smtp.163.com&#39; msg = MIMEText(&#39;python send email test...&#39;,&#39;plain&#39;,&#39;utf-8&#39;) msg[&#39;From&#39;] = &#39;abc&lt;%s&gt;&#39;% from_addr msg[&#39;To&#39;] = &#39;test@test.com&#39; msg[&#39;Subject&#39;] = &#39;Python邮件测试&#">
<meta itemprop="datePublished" content="2019-06-06T10:10:10+08:00" />
<meta itemprop="dateModified" content="2019-06-06T10:10:10+08:00" />
<meta itemprop="wordCount" content="1029">



<meta itemprop="keywords" content="python,email," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="python收发邮件"/>
<meta name="twitter:description" content="发送邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import smtplib from email.mime.text import MIMEText from_addr = &#39;&#39; password = &#39;&#39; to_addr = smtp_server = &#39;smtp.163.com&#39; msg = MIMEText(&#39;python send email test...&#39;,&#39;plain&#39;,&#39;utf-8&#39;) msg[&#39;From&#39;] = &#39;abc&lt;%s&gt;&#39;% from_addr msg[&#39;To&#39;] = &#39;test@test.com&#39; msg[&#39;Subject&#39;] = &#39;Python邮件测试&#"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Lglspace</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Lglspace</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">python收发邮件</h1>

      <div class="post-meta">
        <span class="post-time"> 2019-06-06 </span>
        <div class="post-category">
            <a href="/categories/python/"> python </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#发送邮件">发送邮件</a></li>
        <li><a href="#收取邮件">收取邮件</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="发送邮件">发送邮件</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">smtplib</span>
<span class="kn">from</span> <span class="nn">email.mime.text</span> <span class="kn">import</span> <span class="n">MIMEText</span>

<span class="n">from_addr</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">password</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">to_addr</span> <span class="o">=</span> 
<span class="n">smtp_server</span> <span class="o">=</span> <span class="s1">&#39;smtp.163.com&#39;</span>

<span class="n">msg</span> <span class="o">=</span> <span class="n">MIMEText</span><span class="p">(</span><span class="s1">&#39;python send email test...&#39;</span><span class="p">,</span><span class="s1">&#39;plain&#39;</span><span class="p">,</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;abc&lt;</span><span class="si">%s</span><span class="s1">&gt;&#39;</span><span class="o">%</span> <span class="n">from_addr</span>
<span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;test@test.com&#39;</span>
<span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Python邮件测试&#39;</span>

<span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="n">smtp_server</span><span class="p">,</span><span class="mi">25</span><span class="p">)</span>
<span class="n">server</span><span class="o">.</span><span class="n">login</span><span class="p">(</span><span class="n">from_addr</span><span class="p">,</span><span class="n">password</span><span class="p">)</span>
<span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="n">from_addr</span><span class="p">,</span><span class="n">to_addr</span><span class="p">,</span><span class="n">msg</span><span class="o">.</span><span class="n">as_string</span><span class="p">())</span>
<span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="收取邮件">收取邮件</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span><span class="lnt">118
</span><span class="lnt">119
</span><span class="lnt">120
</span><span class="lnt">121
</span><span class="lnt">122
</span><span class="lnt">123
</span><span class="lnt">124
</span><span class="lnt">125
</span><span class="lnt">126
</span><span class="lnt">127
</span><span class="lnt">128
</span><span class="lnt">129
</span><span class="lnt">130
</span><span class="lnt">131
</span><span class="lnt">132
</span><span class="lnt">133
</span><span class="lnt">134
</span><span class="lnt">135
</span><span class="lnt">136
</span><span class="lnt">137
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">email.header</span> <span class="kn">import</span> <span class="n">decode_header</span>
<span class="kn">from</span> <span class="nn">email.parser</span> <span class="kn">import</span> <span class="n">Parser</span>
<span class="kn">from</span> <span class="nn">email.utils</span> <span class="kn">import</span> <span class="n">parseaddr</span>
<span class="kn">from</span> <span class="nn">poplib</span> <span class="kn">import</span> <span class="n">POP3</span>

<span class="c1"># 解析消息头中的字符串</span>
<span class="c1"># 没有这个函数，print出来的会使乱码的头部信息。如&#39;=?gb18030?B?yrXWpL3hufsueGxz?=&#39;这种</span>
<span class="c1"># 通过decode，将其变为中文</span>
<span class="k">def</span> <span class="nf">decode_str</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">value</span><span class="p">,</span> <span class="n">charset</span> <span class="o">=</span> <span class="n">decode_header</span><span class="p">(</span><span class="n">s</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">charset</span><span class="p">:</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">charset</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">value</span>


<span class="c1"># 解码邮件信息分为两个步骤，第一个是取出头部信息</span>
<span class="c1"># 首先取头部信息</span>
<span class="c1"># 主要取出[&#39;From&#39;,&#39;To&#39;,&#39;Subject&#39;]</span>

<span class="c1"># 如上述样式，均需要解码</span>
<span class="k">def</span> <span class="nf">get_header</span><span class="p">(</span><span class="n">emailNo</span><span class="p">,</span><span class="n">msg</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">header</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">,</span> <span class="s1">&#39;To&#39;</span><span class="p">,</span> <span class="s1">&#39;Subject&#39;</span><span class="p">,</span><span class="s1">&#39;Date&#39;</span><span class="p">]:</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">header</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value</span><span class="p">:</span>
            <span class="c1"># 文章的标题有专门的处理方法</span>
            <span class="k">if</span> <span class="n">header</span> <span class="o">==</span> <span class="s1">&#39;Subject&#39;</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">decode_str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">header</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">,</span> <span class="s1">&#39;To&#39;</span><span class="p">]:</span>
                <span class="c1"># 地址也有专门的处理方法</span>
                <span class="n">hdr</span><span class="p">,</span> <span class="n">addr</span> <span class="o">=</span> <span class="n">parseaddr</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="n">name</span> <span class="o">=</span> <span class="n">decode_str</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span>
                <span class="c1"># value = name + &#39; &lt; &#39; + addr + &#39; &gt; &#39;</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">name</span>
            <span class="k">elif</span> <span class="n">header</span> <span class="o">==</span> <span class="s1">&#39;Date&#39;</span><span class="p">:</span>
                <span class="n">utcstr</span> <span class="o">=</span> <span class="n">value</span>
                <span class="k">if</span> <span class="s1">&#39;+0000&#39;</span> <span class="ow">in</span> <span class="n">utcstr</span><span class="p">:</span>
                    <span class="n">regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">&#39;.+?,\s*&#39;</span><span class="p">)</span>
                    <span class="n">utcstr</span> <span class="o">=</span> <span class="n">regex</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">utcstr</span><span class="p">)</span>
                    <span class="n">localdatetime</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">utcstr</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%d</span><span class="s1"> %b %Y %H:%M:%S +0000&#39;</span><span class="p">)</span> \
                                    <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=+</span><span class="mi">8</span><span class="p">)</span>
                <span class="k">elif</span> <span class="s1">&#39;+0800&#39;</span> <span class="ow">in</span> <span class="n">utcstr</span><span class="p">:</span>
                    <span class="n">utcstr</span> <span class="o">=</span> <span class="n">utcstr</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; (CST)&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
                    <span class="n">regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">&#39;.+?,\s*&#39;</span><span class="p">)</span>
                    <span class="n">utcstr</span> <span class="o">=</span> <span class="n">regex</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">utcstr</span><span class="p">)</span>
                    <span class="n">localdatetime</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">utcstr</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%d</span><span class="s1"> %b %Y %H:%M:%S +0800&#39;</span><span class="p">)</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">localdatetime</span>
        <span class="k">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">emailNo</span><span class="p">)</span><span class="o">+</span><span class="s1">&#39;-&gt;&#39;</span><span class="o">+</span><span class="n">header</span> <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>


<span class="c1"># 头部信息已取出</span>


<span class="c1"># 获取邮件的字符编码，首先在message中寻找编码，如果没有，就在header的Content-Type中寻找</span>
<span class="k">def</span> <span class="nf">guess_charset</span><span class="p">(</span><span class="n">msg</span><span class="p">):</span>
    <span class="n">charset</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_charset</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">charset</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">content_type</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">content_type</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;charset=&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">pos</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">charset</span> <span class="o">=</span> <span class="n">content_type</span><span class="p">[</span><span class="n">pos</span> <span class="o">+</span> <span class="mi">8</span><span class="p">:]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">charset</span> <span class="k">if</span> <span class="n">charset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="k">else</span> <span class="s2">&#34;gb2312&#34;</span>


<span class="c1"># 邮件正文部分</span>
<span class="c1"># 取附件</span>
<span class="c1"># 邮件的正文部分在生成器中，msg.walk()</span>
<span class="c1"># 如果存在附件，则可以通过.get_filename()的方式获取文件名称</span>
<span class="k">def</span> <span class="nf">get_resume_html</span><span class="p">(</span><span class="n">revcSer</span><span class="p">,</span> <span class="n">emailNo</span><span class="p">):</span>
    <span class="n">rsp</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">size</span> <span class="o">=</span> <span class="n">revcSer</span><span class="o">.</span><span class="n">retr</span><span class="p">(</span><span class="n">emailNo</span><span class="p">)</span>

    <span class="c1"># lines存储了邮件的原始文本的每一行,</span>
    <span class="c1"># 可以获得整个邮件的原始文本:</span>
    <span class="n">msg_content</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\r\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;gb2312&#39;</span><span class="p">)</span>
    <span class="c1"># 稍后解析出邮件:</span>
    <span class="n">msg</span> <span class="o">=</span> <span class="n">Parser</span><span class="p">()</span><span class="o">.</span><span class="n">parsestr</span><span class="p">(</span><span class="n">msg_content</span><span class="p">)</span>
    <span class="n">charset</span> <span class="o">=</span> <span class="n">guess_charset</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
    <span class="n">get_header</span><span class="p">(</span><span class="n">emailNo</span><span class="p">,</span><span class="n">msg</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">msg</span><span class="o">.</span><span class="n">walk</span><span class="p">():</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">get_filename</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">filename</span> <span class="o">!=</span> <span class="bp">None</span><span class="p">:</span>  <span class="c1"># 如果存在附件</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">decode_str</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>  <span class="c1"># 获取的文件是乱码名称，通过一开始定义的函数解码</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">get_payload</span><span class="p">(</span><span class="n">decode</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>  <span class="c1"># 取出文件正文内容</span>

            <span class="k">if</span> <span class="n">filename</span> <span class="o">==</span> <span class="s1">&#39;resume.html&#39;</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="n">charset</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">continue</span>
    <span class="k">return</span> <span class="s2">&#34;&#34;</span>


<span class="k">def</span> <span class="nf">get_content</span><span class="p">(</span><span class="n">msg</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">msg</span><span class="o">.</span><span class="n">walk</span><span class="p">():</span>
        <span class="n">content_type</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">get_content_type</span><span class="p">()</span>
        <span class="n">charset</span> <span class="o">=</span> <span class="n">guess_charset</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>
        <span class="c1"># 如果有附件，则直接跳过</span>
        <span class="k">if</span> <span class="n">part</span><span class="o">.</span><span class="n">get_filename</span><span class="p">()</span> <span class="o">!=</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">email_content_type</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">content</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">content_type</span> <span class="o">==</span> <span class="s1">&#39;text/plain&#39;</span><span class="p">:</span>
            <span class="n">email_content_type</span> <span class="o">=</span> <span class="s1">&#39;text&#39;</span>
        <span class="k">elif</span> <span class="n">content_type</span> <span class="o">==</span> <span class="s1">&#39;text/html&#39;</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s1">&#39;html 格式 跳过&#39;</span><span class="p">)</span>
            <span class="k">continue</span>  <span class="c1"># 不要html格式的邮件</span>
            <span class="n">email_content_type</span> <span class="o">=</span> <span class="s1">&#39;html&#39;</span>
        <span class="k">if</span> <span class="n">charset</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">content</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">get_payload</span><span class="p">(</span><span class="n">decode</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">charset</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s1">&#39;type error&#39;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">LookupError</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s2">&#34;unknown encoding: utf-8&#34;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">email_content_type</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">continue</span>
            <span class="c1"># 如果内容为空，也跳过</span>
        <span class="k">print</span><span class="p">(</span><span class="n">email_content_type</span> <span class="o">+</span> <span class="s1">&#39; -----  &#39;</span> <span class="o">+</span> <span class="n">content</span><span class="p">)</span>


<span class="c1"># get_file(msg)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">emailInfo</span> <span class="o">=</span> <span class="p">{};</span>
    <span class="n">revcSer</span> <span class="o">=</span> <span class="n">POP3</span><span class="p">(</span><span class="n">emailInfo</span><span class="p">[</span><span class="s2">&#34;popServer&#34;</span><span class="p">])</span>
    <span class="n">revcSer</span><span class="o">.</span><span class="n">user</span><span class="p">(</span><span class="n">emailInfo</span><span class="p">[</span><span class="s2">&#34;username&#34;</span><span class="p">])</span>
    <span class="n">revcSer</span><span class="o">.</span><span class="n">pass_</span><span class="p">(</span><span class="n">emailInfo</span><span class="p">[</span><span class="s2">&#34;passwd&#34;</span><span class="p">])</span>

    <span class="n">response</span> <span class="o">=</span> <span class="n">revcSer</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span>
    <span class="n">maxSize</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">response</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">revcSer</span><span class="o">.</span><span class="n">capa</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">emailNo</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">maxSize</span><span class="p">,</span><span class="n">maxSize</span><span class="o">-</span><span class="mi">50</span> <span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">resumHtml</span> <span class="o">=</span> <span class="n">get_resume_html</span><span class="p">(</span><span class="n">revcSer</span><span class="p">,</span> <span class="n">emailNo</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">resumHtml</span> <span class="o">!=</span> <span class="s2">&#34;&#34;</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="n">resumHtml</span><span class="p">)</span>
            <span class="k">break</span>
    <span class="n">revcSer</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">lglspace</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2019-06-06
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/python/">python</a>
          <a href="/tags/email/">email</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/mongodb/mongodb-relate/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">mongodb相关备忘</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/python/python-yaml/">
            <span class="next-text nav-default">python解析yaml</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:lglspace@outlook.com" class="iconfont icon-email" title="email"></a>
  <a href="https://lglspace.cn/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  

  

  <span class="copyright-year">
    &copy; 
    2020 - 
    2021<span class="heart"><i class="iconfont icon-heart"></i></span><span>lglspace</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.a74e82b84b872f6b028df4e965781ddbba1606f0253b4fad5e4cf3913cf576eb.js"></script>






<script src="/js/site-custom.js"></script>


</body>
</html>
